﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using UFIDA.U9.PM.RCV;
using UFSoft.UAP.Report.Base;
using UFSoft.UBF.Analysis.Interface.MD.Report.Model;
using UFSoft.UBF.Analysis.Interface.MD.Report.Service;
using UFSoft.UBF.Analysis.MD.Report.Model;
using UFSoft.UBF.Analysis.MD.Report.Service;
using UFSoft.UBF.ExportService;
using UFSoft.UBF.Util.Context;
using UFSoft.UBF.Util.DataAccess;

namespace ufida.u9.ait.openapi.utils
{
    //需引用DLL清单：
    //UFSoft.UBF.Report.Base.dll
    //UFSoft.UBF.Analysis.Interface.dll
    //UFSoft.UBF.Analysis.MD.Report.dll
    //UFSoft.UBF.ExportService.dll

    /// <summary>
    /// U9打印处理类
    /// </summary>
    public class PrintUtils
    {
        /// <summary>
        /// 按照U9打印模板,输出PDF文件,返回文件路径
        /// </summary>
        /// <param name="printCatalog">模板目录</param>
        /// <param name="printTmplID">模板ID</param>
        /// <param name="dataView">数据视图</param>
        /// <param name="dataPath">查询PATH</param>
        /// <returns>PDF文件路径</returns>
        public static string ExportPDF(string printCatalog,string printTmplID,string dataView,string dataPath)
        {
            //string printCatalog = "U9.SCM.PM.POUI"; //打印目录的实体Key
            //string printTmplID = "5634e561-e22f-4200-aafa-ca3ecc55c524"; //模板标识符

            //打印设置
            IExportSettings settings = ExportServiceFactory.GetInstance().CreateExportSettingsObject();
            settings.ExportStyle = enumExportStyle.PDF;
            settings.PrintTemplateCatalogType = printCatalog;
            settings.FileName = dataView; //输出文件名

            //打印模板
            IReportTemplate report = GetReportTemplateByID(printTmplID);
            //打印数据
            DataSet ds = GetPrintData(dataView,dataPath);
            //打印输出
            string file = Export(report, ds, settings);

            return file;
        }

        /// <summary>
        /// 获取打印数据
        /// </summary>
        /// <returns></returns>
        private static DataSet GetPrintData(String dataView,string dataPath)
        {
            DataSet ds = new DataSet();
            string sql = $"select * from {dataView} where 1=1";
            if (!string.IsNullOrEmpty(dataPath))
            {
                sql += " and " + dataPath;
            }
            DataAccessor.RunSQL(DataAccessor.GetConn(), sql, null, out ds);
            return ds;
        }

        /// <summary>
        /// 按指定的模板、数据集、设置生成文件
        /// </summary>
        /// <param name="reportTemplate">打印模板</param>
        /// <param name="printData">打印数据</param>
        /// <param name="settings">打印设置</param>
        /// <returns>输出文件的物理路径</returns>
        private static string Export(IReportTemplate reportTemplate, DataSet printData, IExportSettings settings)
        {
            string result = string.Empty;

            IExportContext context = ExportServiceFactory.GetInstance().CreateContextObject(PlatformContext.Current.UserName);
            IExportService exportService = ExportServiceFactory.GetInstance().GetExportService(context);

            exportService.ExportTemplate(reportTemplate, printData, settings);

            if (string.IsNullOrEmpty(settings.ErrorMessage) && string.IsNullOrEmpty(settings.ErrorMessageForBC))
            {
                result = settings.ExportResultFile.PhysicsPath;
            }
            else
            {
                throw new Exception(settings.ErrorMessage);
            }

            return result;
        }

        /// <summary>
        /// 依据打印模板ID获取打印模板
        /// </summary>
        /// <param name="printTemplateID">模板ID</param>
        /// <returns></returns>
        private static IReportTemplate GetReportTemplateByID(string printTemplateID)
        {
            string connectionString = DBConnectionHelper.GetConnectionString();
            string cultrueName = PlatformContext.Current.Culture;
            if (string.IsNullOrEmpty(cultrueName))
            {
                cultrueName = "zh-CN";
            }

            ReportMDService service = ReportMDService.GetInstance(connectionString, cultrueName);
            IReportMDReader reader = service.GetReader();

            IReportTemplate reportTemplate = reader.GetReportTemplateByID(printTemplateID);
            reader.Dispose();

            if (reportTemplate == null) throw new Exception("没有找到打印模板！");
            return reportTemplate;
        }
    }
}
